package com.lyn.dwjhb.convertor;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;

import com.lyn.dwjhb.model.SysUser;

public class UserConvertor extends BaseConvertor{
	public UserConvertor() {
		super();
	}
	
	public UserConvertor(QueryRunner rq) {
		super(rq);
	}



	public static int sorting = 100;
	private String getInsertSql(SysUser sysUser) {
		String sql = "insert into sys_user (guid, gmt_create, gmt_modified, "
				+ "is_active, sorting, "
				+ "user_name, dept_id) values("
				+ wrapper(sysUser.getUserId())+", sysdate, sysdate, 1, "+sorting+","+
				wrapper(sysUser.getUserName())+", "
				+ wrapper(sysUser.getDeptId())+");";
		sorting+=10;
		return sql;
	}
	
	
	
	public void toFile(List<SysUser> list) {
		File file = new File("resource/PROC_INSERT_SYS_USER.sql");
		if(!file.exists()) {
			file.delete();
		}
        try {
        	file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        try(
            FileOutputStream fos = new FileOutputStream(file);
            OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
        	BufferedWriter bw = new BufferedWriter(osw);) {
        	String sql = "create or replace procedure PROC_INSERT_SYS_USER is";
        	this.writeLine(bw, sql);
        	sql = "begin";
        	this.writeLine(bw, sql);
        	sql = "insert into sys_user (guid, gmt_create, gmt_modified, user_name, password, "
        			+ "dept_id, real_name, email, phone, is_active, sorting) values("
        			+ "'uid705db7bf13b40bcc10a7fa9', sysdate, sysdate, 'admin', 'C4CA4238A0B923820DCC509A6F75849B', "
        			+ "'9ecb4266ea8146a09a40dedf0928319d', '管理员', 'admin@qq.com', '0771-2551699', 1, 90);";
        	this.writeLine(bw, sql);
        	
        	sql = "insert into sys_user (guid, gmt_create, gmt_modified, user_name, password, "
        			+ "dept_id, real_name, email, phone, is_active, sorting) values("
        			+ "'uid705db7bf13b40bcc10a7bb8', sysdate, sysdate, 'liang_b', 'C4CA4238A0B923820DCC509A6F75849B', "
        			+ "'9ecb4266ea8146a09a40dedf0928319d', '管理员', 'liang_b@qq.com', '0771-2551698', 1, 91);";
        	
        	this.writeLine(bw, sql);
        	sql = "insert into sys_user (guid, gmt_create, gmt_modified, user_name, password, "
        			+ "dept_id, real_name, email, phone, is_active, sorting) values("
        			+ "'uid705db7bf13b40bcc10a7cc7', sysdate, sysdate, 'xie_jj', 'C4CA4238A0B923820DCC509A6F75849B', "
        			+ "'9ecb4266ea8146a09a40dedf0928319d', '管理员', 'xie_jj@qq.com', '0771-2551697', 1, 92);";
        	this.writeLine(bw, sql);
        	
        	for(SysUser sysUser : list) {
        		sql = getInsertSql(sysUser);
        		this.writeLine(bw, sql);
    		}
        	sql = "commit;";
        	this.writeLine(bw, sql);
        	sql = "end PROC_INSERT_SYS_USER;";
        	this.writeLine(bw, sql);
        } catch (Exception e) {
            e.printStackTrace(); 
        } 
	}
	
	
	
}
